package com.example.springboot_demo.controller;

import com.example.springboot_demo.entity.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/9/2:16:37
 */
//http://localhost:8080/student/findAll
//http://localhost:8080/student/findById
@SuppressWarnings({"unchecked", ""})
@Controller
@RequestMapping("/student")
public class StudentMsgController {

    private static final Map<Integer, Student> STUDENT_MAP = new HashMap<>();

    static {
        for (int i = 0; i < 10; i++) {
            Student student = new Student(i, "name" + i,1+i);
            STUDENT_MAP.put(i,student);
        }
    }

    private static final Logger log = LoggerFactory.getLogger(StudentMsgController.class);

    /**
     * 根据id 修改信息
    */
    @PostMapping("/updateStudent")
    @ResponseBody
    public Object updateStudent(@RequestBody Student student){
        if (!STUDENT_MAP.containsKey(student.getStuId())) {
            return "学生不存在";
        }
        STUDENT_MAP.put(student.getStuId(),student);
        return "修改成功";
    }

    /**
     * 新增信息
     */
    @PostMapping("/addStudent")
    @ResponseBody
    public Object addStudent(@RequestBody Student student){
        STUDENT_MAP.put(student.getStuId(),student);
        return "新增成功";
    }


    /**
     * 删除信息 id
    */
    @GetMapping("/deleteById")
    @ResponseBody
    public Object deleteById(Integer stuId){
        Student remove = STUDENT_MAP.remove(stuId);
        if(remove==null){
            return "删除失败 用户不存在";
        }
        return "删除成功";
    }





    /**
     * 查询所有学生
    */
    @RequestMapping("/findAll")
    @ResponseBody
    public Object findAll(){
        return STUDENT_MAP;
    }

    /**
     * 根据学号查询学生信息
     */
    @RequestMapping("/findById")
    @ResponseBody
    public Student findById(Integer stuId){
        Student student = STUDENT_MAP.get(stuId);
        return student;
    }


}
